package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.entity.Permission;
import com.atguigu.service.PermissionService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/permission")
public class PermissionController {

    @Reference
    private PermissionService permissionService;


    @RequestMapping
    public String index(Model model){
        //查询到所有菜单权限,也就是permission表里的全部数据,存入共享域,渲染转发
        //注意这里的菜单权限是需要树形结构上下级的.
        List<Permission> list = permissionService.findAllMenu();
        model.addAttribute("list",list);
        return "permission/index";
    }

    @RequestMapping("/create")
    public String create(Permission permission, Model model){
        model.addAttribute("permission",permission);
        return "permission/create";
    }

    @RequestMapping("/save")
    public String save(Permission permission){
        permissionService.insert(permission);
        return "common/successPage";
    }

    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long permissionId,Model model){
        //根据permissionId查找到权限数据,保存到域里进行数据回显
        Permission permission = permissionService.getById(permissionId);
        model.addAttribute("permission",permission);
        return "permission/edit";
    }

    @RequestMapping("/update")
    public String update(Permission permission){
        permissionService.update(permission);
        return "common/successPage";
    }

    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") Long permissionId){
        permissionService.delete(permissionId);
        return "redirect:/permission";
    }

}
